package com.workflow.engine.demo.utils;

import java.text.ParseException;

import static com.workflow.engine.core.utils.DateUtil.format;
import static com.workflow.engine.core.utils.DateUtil.parse;

/**
 * Created by houjinxin on 2016/12/31.
 */
public class ConfigMethodUtil {

    /**
     * 获取身份证后六位
     *
     * @param identity
     * @return
     */
    public static String getLastSixNumberFromIdentity(String identity) {
        String lastSixNumber;
        if (identity.length() == 15) {
            lastSixNumber = identity.substring(9, 15);
        } else {
            lastSixNumber = identity.substring(12, 18);
        }
        return lastSixNumber;
    }

    /**
     * 获取指定格式的字符串型生日
     *
     * @param identity
     * @return
     */
    public static String getBirthdayFromIdentity(String identity, String pattern) throws ParseException {
        String birthDay;
        if (identity.length() == 15) {
            birthDay = "19" + identity.substring(6, 12);
        } else {
            birthDay = identity.substring(6, 14);
        }
        return format(parse(birthDay, "yyyyMMdd"), pattern);
    }

    /**
     * 获取性别,各个保险公司性别对应的值不一样,故后两个参数分别表示保险公司对应的男女标识
     *
     * @param identity 身份证
     * @param male 男性标识
     * @param female 女性标识
     * @return
     */
    public static String getGenderFromIdentity(String identity, String male, String female) {
        String gender;
        if (identity.length() == 15) {
            gender = Integer.valueOf(identity.substring(14, 15)) % 2 == 0 ? female : male;
        } else {
            gender = Integer.valueOf(identity.substring(16, 17)) % 2 == 0 ? female : male;
        }
        return gender;
    }

    /**
     * 获取发动机后4位
     * @param engineNo
     * @return
     */
    public static String getEngineNoLastFourNumber(String engineNo) {
        return engineNo.substring(engineNo.length() - 4, engineNo.length());
    }

    /**
     * 截取字符串中的中文
     *
     * @param content 输入的内容
     * @return 中文
     */
    public static String extractChinese(String content) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < content.length(); i++) {
            String retContent = content.substring(i, i + 1);
            boolean isChina = retContent.matches("[\u4E00-\u9FA5]");
            if (isChina) {
                sBuffer.append(retContent);
            }
        }
        return sBuffer.toString();
    }

    /**
     * 获取日期年月
     * @param date 日期
     * @param separator 分割符, 有的日期用"-",有的日期用"/"
     * @return
     */
    public static String getYearAndMonthFromDate(String date, String separator){
        return date.replaceAll(separator, "").substring(0,6);
    }

    /**
     * 生成随机电话号码
     *
     * @return
     */
    public static String getTel() {
        String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
        int index = getRandomNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getRandomNum(1, 888) + 10000).substring(1);
        String third = String.valueOf(getRandomNum(1, 9100) + 10000).substring(1);
        return first + second + third;
    }

    /**
     * 生成随机数
     *
     * @param start
     * @param end
     * @return
     */
    private static int getRandomNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }
}
